-- WaitCondition
--
local trace = trace
local import = import
---@type behavior_ret
local bret = import('bt/bt_plugin/behavior3/behavior_ret')

local M = {
    name = "WaitCondition",
    type = "Decorator",
    desc = "等待条件满足",
    args = {
        {
            name = 'time',
            type = 'int',
            desc = '时间/tick'
        },
        {
            name = 'setDeltaTime',
            type = 'int?',
            desc = '设置间隔时间'
        }
    },
    doc = [[
        + 只能有一个子节点,多个仅执行第一个
        + 只有成功会跳出验证
        + 验证次数超过会返回失败
        + 子节点不可有RUNNING状态

    ]]
}

function M.run(node, env, value)
    local args = node.args
    local ctx = env.ctx
    local setDeltaTime = args.setDeltaTime
    local time = args.time
    local children = node.children[1]
    local info = children.info
    if setDeltaTime then
        ctx:setDeltaTime(setDeltaTime)
    end
    local t = node:resume(env)
    if t then
        if t <= 0 then
            trace.log_info(string.format('【%s】:验证次数超过', info))
            return bret.FAIL
        end
    else
        t = time
    end
    local r = children:run(env)
    if r == bret.SUCCESS then
        trace.log_info(string.format('【%s】:条件满足退出验证', info))
        return bret.SUCCESS
    else
        trace.log_info(string.format('【%s】:条件没有满足继续验证', info))
    end
    return node:yield(env, t - 1)
end

return M
